#include <stdio.h>

#include "../GXL_Engine/gx_engine.h"

class MyThread:public GXL_Engine::Runnable
{
public:
	MyThread(const char* szName)
		:m_szName(_strdup(szName)){}

private:
	VOID Run()
	{
		printf("%s start sleep\n", m_szName);
		Sleep(100);
	}
	const char* m_szName;
};

class MyProductor:public GXL_Engine::Runnable
{
public:
	MyProductor(GXL_Engine::Thread* pThread)
		:m_pThread(pThread)	{}
	VOID Run()
	{
		for (INT i=0; i<50; ++i)
		{
			char buffer[100] = {0};
			sprintf_s(buffer, "Task%d", i);
			MyThread* pMyThread = new MyThread(buffer);
			m_pThread->AddTask(pMyThread);
		}
	}
private:
	GXL_Engine::Thread* m_pThread;
};


class ThreaderExp:public GXL_Engine::Threader<ThreaderExp>
{
public:
	VOID	Fun()
	{
		printf("Fun Begin\n");
		Sleep(1000);
		printf("Fun End\n");
	}
	VOID	Fun2()
	{
		printf("Fun2 Begin\n");
		Sleep(1000);
		printf("Fun2 End\n");
	}
};

int test_thread2()
{
	ThreaderExp thr;
	thr.StartThread(&ThreaderExp::Fun, "Fun");
	thr.StartThread(&ThreaderExp::Fun2, "Fun2");

	thr.WaitThread("Fun");
	Sleep(20);
	thr.WaitThread("Fun2");

	return 0;
}

int test_thread()
{
	GXL_Engine::Thread conThread;
	MyProductor productor(&conThread);
	MyProductor productor1(&conThread);
	GXL_Engine::Thread proThread;
	proThread.AddTask(&productor);
	proThread.AddTask(&productor1);


	Sleep(10000);
	proThread.ShutDown();
	conThread.ShutDown();

	proThread.Join();
	conThread.Join();

	return 0;
}